﻿Imports quanlydulich.Helpers
Imports DevComponents.DotNetBar

Public Class MainForm
    Friend WithEvents frmKetNoiCSDL As FrmKetNoiCSDL
    Friend WithEvents frmDangNhap As FrmDangNhap
    Friend WithEvents frmTimTour As TIMKIEMTOUR
    Friend WithEvents frmTimKH As TIMKIEMKHACHHANG
    Friend WithEvents frmTimNV As TIMKIEMNHANVIEN
    Friend WithEvents frmChonTour As CHONTOUR

    Private Sub menChucVu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menChucVu.Click
        Dim frm As CHUCVU = New CHUCVU
        frm.ShowDialog()
    End Sub

    Private Sub frmKetNoiCSDL_Handler(ByVal sender As Object, ByVal e As MyConnectionEvent) Handles frmKetNoiCSDL.Handler
        Dim _provider As New XMLProvider()

        Dim eventType = DirectCast(e, MyConnectionEvent)

        GlobalVars.g_strDataSource = eventType.DataSource
        GlobalVars.g_strDataBaseName = eventType.DataBaseName

        ' Create config file
        If Boolean.Parse(eventType.ConnStatus) Then
            _provider.XMLWriter(GlobalVars.g_strPathConfig, eventType.DataSource, eventType.DataBaseName, eventType.ConnStatus)
        Else
            _provider.XMLWriter(GlobalVars.g_strPathConfig, eventType.DataSource, eventType.DataBaseName, eventType.UserName, eventType.Password, eventType.ConnStatus)
        End If
    End Sub

    Private Sub frmDangNhap_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmDangNhap.Handler
        Dim success As Boolean = True
        Dim infoText As String = ""
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        Dim username As String = comp(0).Trim()
        Dim password As String = comp(1).Trim()
        Dim PasswordEnc As String = Encryption.Encrypt(password)

        Dim objNhanVienBUS As NHANVIENBUS = New NHANVIENBUS(GlobalVars.g_strConnectionString)

        Dim lstItem As DataTable = objNhanVienBUS.SelectByUserName(username).Tables(0)

        If lstItem.Rows.Count = 0 Then
            success = False
            infoText = "Người dùng này chưa có trong hệ thống"
        Else
            objNhanVienBUS = New NHANVIENBUS(GlobalVars.g_strConnectionString)
            lstItem = objNhanVienBUS.SelectByUserNameAndPassword(username, PasswordEnc).Tables(0)
            If lstItem.Rows.Count = 0 Then
                success = False
                infoText = "Bạn đã nhập sai mật khẩu"
            Else
                GlobalVars.g_CurrentUser = New NHANVIENDTO()
                GlobalVars.g_CurrentUser.ID = Integer.Parse(lstItem.Rows(0)("ID").ToString())
                GlobalVars.g_CurrentUser.IDLoaiNguoiDung = Integer.Parse(lstItem.Rows(0)("IDLoaiNguoiDung").ToString())
                GlobalVars.g_CurrentUser.IDCV = Integer.Parse(lstItem.Rows(0)("IDCV").ToString())
                GlobalVars.g_CurrentUser.TenNV = lstItem.Rows(0)("TenNV").ToString()
                GlobalVars.g_CurrentUser.NgaySinh = DateTime.Parse(lstItem.Rows(0)("NgaySinh").ToString())
                GlobalVars.g_CurrentUser.GioiTinh = IIf(lstItem.Rows(0)("GioiTinh").ToString() = "False", 0, 1)
                GlobalVars.g_CurrentUser.DiaChi = lstItem.Rows(0)("DiaChi").ToString()
                GlobalVars.g_CurrentUser.SoDT = lstItem.Rows(0)("SoDT").ToString()
                GlobalVars.g_CurrentUser.TenDangNhap = lstItem.Rows(0)("TenDangNhap").ToString()
                GlobalVars.g_CurrentUser.MatKhau = lstItem.Rows(0)("MatKhau").ToString()
                infoText = "Chào bạn  " & GlobalVars.g_CurrentUser.TenNV & "  vào hệ thống !"
            End If
        End If

        If Not success Then
            MessageBox.Show(infoText, "Log In", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            ' Re-login
            Dim frm As New FrmDangNhap()
            frm.ShowDialog()
        Else
            menDangNhap.Enabled = False

            Dim dictFucns = GlobalUserManagers.InitFuncsMapping()
            ' Store list of functionalities that user allow using
            Dim funcsAllowUsing = GlobalUserManagers.InitFuncsAllowUsing()

            ' Disable some functions that user not allow using
            For Each item As Object In dictFucns
                For Each item2 As String In funcsAllowUsing
                    If item.Value = item2 Then
                        GlobalUserManagers.EnableFuncAllowUsing(pnlMain, item.Key)
                        Exit For
                    End If
                Next
            Next

            MessageBoxEx.Show(infoText)
        End If
    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ErrorText As String = ""

        If Not System.IO.File.Exists(GlobalVars.g_strPathConfig) Then
            frmKetNoiCSDL = New FrmKetNoiCSDL
            frmKetNoiCSDL.ShowDialog()
        Else
            Dim _provider As New XMLProvider()
            ' Load config file to get infos that used to connect to DB
            _provider.GetConnectionInfo(ErrorText)
            If ErrorText <> "" Then
                MessageBoxEx.Show(ErrorText, "ERROR", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.[Error])
                ' Re-create connection infos
                Dim frm As New FrmKetNoiCSDL()
                frm.ShowDialog()
            Else
                ' Get connection string successfull, then open connection
                GlobalVars.g_strConnectionString = "Data Source=" + GlobalVars.g_strDataSource + ";Initial Catalog=" + GlobalVars.g_strDataBaseName + ";Integrated Security=True"
                Dim objNhanVienBUS As NHANVIENBUS = New NHANVIENBUS(GlobalVars.g_strConnectionString)
                Dim lstNguoiDung As DataTable = objNhanVienBUS.SelectAll().Tables(0)
                If lstNguoiDung.Rows.Count > 0 Then
                    frmDangNhap = New FrmDangNhap()
                    frmDangNhap.ShowDialog()
                Else
                    ' Chua co nguoi dung nao trong he thong, nguoi dung dau tien se giu vai tro la admin
                    menQuanLyLoaiNguoiDung.Enabled = True
                    menQuanLyNguoiDung.Enabled = True
                End If
            End If
        End If

    End Sub

    Private Sub menQuanLyLoaiNguoiDung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menQuanLyLoaiNguoiDung.Click
        LOAINGUOIDUNG.ShowDialog()
    End Sub

    Private Sub menQuanLyNguoiDung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menQuanLyNguoiDung.Click
        Dim frm As NHANVIEN = New NHANVIEN
        frm.ShowDialog()
    End Sub

    Private Sub menDoiMatKhau_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menDoiMatKhau.Click
        Dim frm As FrmDoiMatKhau = New FrmDoiMatKhau
        frm.ShowDialog()
    End Sub

    Private Sub menDangNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menDangNhap.Click
        frmDangNhap = New FrmDangNhap()
        frmDangNhap.ShowDialog()
    End Sub

    Public Sub DisableCtrls()
        menDoiMatKhau.Enabled = False
        menQuanLyLoaiNguoiDung.Enabled = False
        menQuanLyNguoiDung.Enabled = False
        menChucVu.Enabled = False
        menTour.Enabled = False
        menKhachSan.Enabled = False
        menNhaHang.Enabled = False
        menDiaDiem.Enabled = False
        menPhuongTien.Enabled = False
        menKhachHang.Enabled = False
        menLoaiVe.Enabled = False
        menTourNhaHang.Enabled = False
        menTourPhuongTien.Enabled = False
        menTourDiaDiem.Enabled = False
        menPhanCongNhanVien.Enabled = False
        menLapPhieuDatTour.Enabled = False
        menDatVe.Enabled = False
        menTimKiemTour.Enabled = False
        menTimKiemKH.Enabled = False
        menTimKiemNV.Enabled = False
        menThongKeDSTour.Enabled = False
        menThongKeDSKhachHang.Enabled = False
        menQuanLyChucNangNhomNguoiDung.Enabled = False
    End Sub

    Private Sub menDangXuat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menDangXuat.Click
        menDangNhap.Enabled = True
        GlobalVars.g_CurrentUser = Nothing
        DisableCtrls()
        frmDangNhap = New FrmDangNhap()
        frmDangNhap.ShowDialog()
    End Sub

    Private Sub menThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menThoat.Click
        Close()
    End Sub

    Private Sub menKhachSan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menKhachSan.Click
        Dim frm As KHACHSAN = New KHACHSAN()
        frm.ShowDialog()
    End Sub

    Private Sub menNhaHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menNhaHang.Click
        Dim frm As NHAHANG = New NHAHANG()
        frm.ShowDialog()
    End Sub

    Private Sub menDiaDiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menDiaDiem.Click
        Dim frm As DIADIEM = New DIADIEM()
        frm.ShowDialog()
    End Sub

    Private Sub menKhachHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menKhachHang.Click
        Dim frm As KHACHHANG = New KHACHHANG()
        frm.ShowDialog()
    End Sub

    Private Sub menPhuongTien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menPhuongTien.Click
        Dim frm As PHUONGTIEN = New PHUONGTIEN
        frm.ShowDialog()
    End Sub

    Private Sub menLoaiVe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menLoaiVe.Click
        Dim frm As LOAIVE = New LOAIVE
        frm.ShowDialog()
    End Sub

    Private Sub menTour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menTour.Click
        Dim frm As TOUR = New TOUR
        frm.ShowDialog()
    End Sub

    Private Sub menTourNhaHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menTourNhaHang.Click
        Dim frm As TOURNHAHANG = New TOURNHAHANG
        frm.ShowDialog()
    End Sub

    Private Sub menTourKhachSan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menTourKhachSan.Click
        Dim frm As TOURKHACHSAN = New TOURKHACHSAN
        frm.ShowDialog()
    End Sub

    Private Sub menTourPhuongTien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menTourPhuongTien.Click
        Dim frm As TOURPHUONGTIEN = New TOURPHUONGTIEN
        frm.ShowDialog()
    End Sub

    Private Sub menTourDiaDiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menTourDiaDiem.Click
        Dim frm As TOURDIADIEM = New TOURDIADIEM
        frm.ShowDialog()
    End Sub

    Private Sub menPhanCongNhanVien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menPhanCongNhanVien.Click
        Dim frm As TOURNHANVIEN = New TOURNHANVIEN
        frm.ShowDialog()
    End Sub

    Private Sub menTimKiemTour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menTimKiemTour.Click
        frmTimTour = New TIMKIEMTOUR
        frmTimTour.ShowDialog()
    End Sub

    Private Sub frmTimTour_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmTimTour.Handler
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        Dim frm As TOUR = New TOUR
        frm.Criterial_ID = comp(0)
        frm.Criterial_Name = comp(1)
        frm.Criterial_FromPrice = comp(2)
        frm.Criterial_ToPrice = comp(3)
        frm.Criterial_StartDate = comp(4)
        frm.Criterial_EndDate = comp(5)
        frm.ModeSearch = True
        frm.ShowDialog()
    End Sub

    Private Sub menTimKiemKH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menTimKiemKH.Click
        frmTimKH = New TIMKIEMKHACHHANG
        frmTimKH.ShowDialog()
    End Sub

    Private Sub frmTimKH_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmTimKH.Handler
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        Dim frm As KHACHHANG = New KHACHHANG
        frm.Criterial_ID = comp(0)
        frm.Criterial_Name = comp(1)
        frm.ModeSearch = True
        frm.ShowDialog()
    End Sub

    Private Sub menTimKiemNV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menTimKiemNV.Click
        frmTimNV = New TIMKIEMNHANVIEN
        frmTimNV.ShowDialog()
    End Sub

    Private Sub frmTimNV_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmTimNV.Handler
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        Dim frm As NHANVIEN = New NHANVIEN
        frm.Criterial_ID = comp(0)
        frm.Criterial_Name = comp(1)
        frm.ModeSearch = True
        frm.ShowDialog()
    End Sub

    Private Sub ttbtnTourDuLich_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ttbtnTourDuLich.Click
        Dim frm As TOUR = New TOUR
        frm.ShowDialog()
    End Sub

    Private Sub ttbtnPhanCongNhanVien_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ttbtnPhanCongNhanVien.Click
        Dim frm As TOURNHANVIEN = New TOURNHANVIEN
        frm.ShowDialog()
    End Sub

    Private Sub ttbtnLapPhieuDatTour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ttbtnLapPhieuDatTour.Click
        frmChonTour = New CHONTOUR
        frmChonTour.ShowDialog()
    End Sub

    Private Sub menLapPhieuDatTour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menLapPhieuDatTour.Click
        frmChonTour = New CHONTOUR
        frmChonTour.ShowDialog()
    End Sub

    Private Sub frmChonTour_Handler(ByVal sender As Object, ByVal e As MyEvent) Handles frmChonTour.Handler
        Dim eventType = DirectCast(e, MyEvent)
        Dim comp As String() = eventType.Data.Split(New Char() {"#"})

        Dim frm As LAPPHIEUDATTOUR = New LAPPHIEUDATTOUR
        frm.MaTour = comp(0)
        frm.TenTour = comp(1)
        frm.ShowDialog()
    End Sub

    Private Sub menDatVe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menDatVe.Click
        Dim frm As VE = New VE
        frm.ShowDialog()
    End Sub

    Private Sub ttbtnDatVe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ttbtnDatVe.Click
        Dim frm As VE = New VE
        frm.ShowDialog()
    End Sub

    Private Sub menQuanLyChucNangNhomNguoiDung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menQuanLyChucNangNhomNguoiDung.Click
        Dim frm As frmQuanLyChucNangNhomNguoiDung = New frmQuanLyChucNangNhomNguoiDung
        frm.ShowDialog()
    End Sub

    Private Sub menThongKeDSTour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menThongKeDSTour.Click
        Dim frm As THONGKETOUR = New THONGKETOUR
        frm.ShowDialog()
    End Sub

    Private Sub menThongKeDSKhachHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menThongKeDSKhachHang.Click
        Dim frm As THONGKEKHACHHANG = New THONGKEKHACHHANG
        frm.ShowDialog()
    End Sub
End Class